package org.jeecg.modules.flow.sys.mytest.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import org.jeecg.common.util.BeanUtil;
import org.jeecg.modules.basic.print.entity.PrintConfig;
import org.jeecg.modules.basic.print.entity.WmsDocumentTypeStatus;
import org.jeecg.modules.basic.print.mapper.PrintConfigMapper;
import org.jeecg.modules.basic.print.service.IWmsDocumentTypeStatusService;
import org.jeecg.modules.flow.sys.mytest.entity.DocumentTypeStatus;
import org.jeecg.modules.flow.sys.mytest.entity.Goal;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    //
 * 创建人员:    zhaojun
 * 创建时间:    2023-11-28
 */
@Service
@AllArgsConstructor
public class GoalServiceImpl implements GoalService {

    private final PrintConfigMapper printConfigMapper;
    private final ISysDictService iSysDictService;
    private final ISysDictItemService iSysDictItemService;
    private final IWmsDocumentTypeStatusService iWmsDocumentTypeStatusService;

    @Override
    public List<Goal> list(String businessType, String documentType) {
        List<Goal> list = new ArrayList<>();
        LambdaQueryWrapper<PrintConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(PrintConfig::getBusinessType, businessType);
        queryWrapper.like(PrintConfig::getDocumentType, documentType);
        List<PrintConfig> printConfigs = printConfigMapper.selectList(queryWrapper);
        printConfigs.stream().forEach(t -> {
            Goal goal = new Goal();
            goal.setBusinessType(t.getBusinessType());
            goal.setDocumentType(t.getDocumentType());
            SysDict one = iSysDictService.getOne(new LambdaQueryWrapper<SysDict>().eq(SysDict::getDictCode, t.getBusinessType()));
            goal.setBusinessName(one.getDictName());
            SysDictItem one1 = iSysDictItemService.getOne(new LambdaQueryWrapper<SysDictItem>().eq(SysDictItem::getDictId, one.getId()).eq(SysDictItem::getItemValue, t.getDocumentType()));
            goal.setDocumentName(one1.getItemText());
            List<WmsDocumentTypeStatus> list1 = iWmsDocumentTypeStatusService.list(
                    new LambdaQueryWrapper<WmsDocumentTypeStatus>()
                            .eq(WmsDocumentTypeStatus::getPrintConfigId, t.getId())
                            .orderByAsc(WmsDocumentTypeStatus::getSort));
            List<DocumentTypeStatus> documentTypeStatuses1 = BeanUtil.copyToList(list1, DocumentTypeStatus.class);
            goal.setDocumentTypeStatuses(documentTypeStatuses1);
            list.add(goal);
        });
        return list;
    }
}
